package com.t.bricks.bussiness.service.customeraccount.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.customeraccount.CustomerAccountDao;
import com.t.bricks.bussiness.db.entity.customeraccount.CustomerAccount;
import com.t.bricks.bussiness.service.customeraccount.CustomerAccountService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.data.MoneyUtil;
import com.t.bricks.utils.function.encode.MD5Util;
import com.t.bricks.utils.function.json.JacksonShell;
import com.t.bricks.utils.function.url.URLUtil;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 顾客账户'CustomerAccount表'基本业务操作类接口实现
 * @author HuangXinBian
 * @DateTime 2022年06月22日 15:25:26
 */
@Service
public class CustomerAccountServiceImpl implements CustomerAccountService<CustomerAccount> {

	@Autowired
	private CustomerAccountDao<CustomerAccount> customerAccountDao;
	
	/**
	 * md5加密干扰串
	 * 注意,必须与BaseSystem中的干扰串一样,否则无法登录
	 */
	@Value("${app.md5.key:未设置app.md5.key配置项}")
	private String appMd5Key;
	
	/**
	 * 创建sRecordKey用的干扰串
	 */
	@Value("${app.record.key.jam:123456}")
	private String recordKeyJam;
	
	/**
	 * 短信验证码查看地址
	 */
	@Value("${app.identifying.code.find:未设置app.identifying.code.find配置项}")
	private String findCodeUrl;
	
	/**
	 * 修改密码
	 * @param sId
	 * @param sOldPass
	 * @param sNewPass
	 * @return
	 */
	@Override
	public MsgEmity editPass(String sId, String sOldPass, String sNewPass) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "用户sId不能为空！", 8001);
		}
		
		if (null == sOldPass || "".equals(sOldPass.trim())) {
			return new MsgEmity(false, "旧密码不能为空！", 8002);
		}
		
		if (null == sNewPass || "".equals(sNewPass.trim())) {
			return new MsgEmity(false, "新密码不能为空！", 8003);
		}

		MsgEmity me = customerAccountDao.hasById(sId);
		if (!me.isSuccess()) {
			return new MsgEmity(false, "没有查找到用户！", 8004);
		}
		
		me = customerAccountDao.findById(sId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到对应用户！", 8005);
		}
		
		CustomerAccount customerAccount = me.getData();
		
		if (customerAccount.getiState() != 1) {
			return new MsgEmity(false, "用户被限制！", 8006);
		}
		
		String sOldPassMd5 = MD5Util.md5(sOldPass, appMd5Key);
		if (!sOldPassMd5.equals(customerAccount.getsPayPass()) ) {
			return new MsgEmity(false, "提供的旧密码错误！", 8007);
		}
		
		String sNewPassMd5 = MD5Util.md5(sNewPass, appMd5Key);
		if (sNewPassMd5.equals(sOldPassMd5)) {
			return MsgEmity.success(8999, "密码无需修改！");
		}

		customerAccount.setsPayPass(sNewPassMd5);
		customerAccount.setsModifieder(ModuleUtil.currentLoginUserId());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setdModifiedDate(new Date());
		
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));//重置记录密钥
		
		return customerAccountDao.editPass(sId,
				sOldPassMd5,
				sNewPassMd5,
				customerAccount.getiVersion(),
				customerAccount.getdModifiedDate(),
				customerAccount.getsModifieder(),
				customerAccount.getsRecordKey()
			);
	}
	
	/**
	 * 重置密码
	 * @param sId 被修改的用户编号
	 * @param sPass 操作人员的密码
	 * @return
	 */
	@Override
	public MsgEmity resetPass(String sId, String sPass) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "用户编号不能为空！", 8001);
		}
		
		sId = sId.trim();
		
		String sUserId = ModuleUtil.currentLoginUserId();
		if (null == sUserId || "".equals(sUserId.trim())) {
			return new MsgEmity(false, "未能获取操作人员信息！", 8002);
		}

		if (!"/00000000/00000001/".contains(sUserId) && !sId.equals(sUserId)) {//超级管理员登录/管理员或当前用户
			return new MsgEmity(false, "仅允许管理员或当前用户重置密码", 8003);
		}
		
		MsgEmity me = customerAccountDao.findLoginPass(sId);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "当前操作用户信息错误,请联系管理员！", 8004);
		}
		
		String loginPass = me.getData();

		String sModifiederPass = MD5Util.md5(sPass, appMd5Key);
		if (!sModifiederPass.equals(loginPass)) {
			return new MsgEmity(false, "提供的操作用户密码错误!", 8005);
		}
		
		me = customerAccountDao.findById(sId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到待重置密码的用户！", 8006);
		}
		
		CustomerAccount customerAccount = me.getData();

		String sOldPass = customerAccount.getsPayPass();
		String sNewPass = MD5Util.md5("123456", appMd5Key);
		if (sOldPass.equals(sNewPass)) {
			return MsgEmity.success(8999, "密码无需修改重置！");
		}

		customerAccount.setsPayPass(sNewPass);
		customerAccount.setsModifieder(sUserId);
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setdModifiedDate(new Date());
		
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));//重置记录密钥
		
		return customerAccountDao.editPass(sId,
				sOldPass,
				sNewPass,
				customerAccount.getiVersion(),
				customerAccount.getdModifiedDate(),
				customerAccount.getsModifieder(),
				customerAccount.getsRecordKey()
			);
	}
	
	/**
	 * 修改手势密码
	 * @param sId
	 * @param sOldPass
	 * @param sNewPass
	 * @return
	 */
	@Override
	public MsgEmity editSignPass(String sId, String sOldPass, String sNewPass) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "用户sId不能为空！", 8001);
		}
		
		if (null == sOldPass || "".equals(sOldPass.trim())) {
			return new MsgEmity(false, "旧密码不能为空！", 8002);
		}
		
		if (null == sNewPass || "".equals(sNewPass.trim())) {
			return new MsgEmity(false, "新密码不能为空！", 8003);
		}

		MsgEmity me = customerAccountDao.hasById(sId);
		if (!me.isSuccess()) {
			return new MsgEmity(false, "没有查找到用户！", 8004);
		}
		
		me = customerAccountDao.findById(sId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到对应用户！", 8005);
		}
		
		CustomerAccount customerAccount = me.getData();
		
		if (customerAccount.getiState() != 1) {
			return new MsgEmity(false, "用户被限制！", 8006);
		}
		
		String sOldPassMd5 = MD5Util.md5(sOldPass, appMd5Key);
		if (!sOldPassMd5.equals(customerAccount.getsPaySignPass()) ) {
			return new MsgEmity(false, "提供的旧密码错误！", 8007);
		}
		
		String sNewPassMd5 = MD5Util.md5(sNewPass, appMd5Key);
		if (sNewPassMd5.equals(sOldPassMd5)) {
			return MsgEmity.success(8999, "密码无需修改！");
		}

		customerAccount.setsPaySignPass(sNewPassMd5);
		customerAccount.setsModifieder(ModuleUtil.currentLoginUserId());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setdModifiedDate(new Date());
		
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));//重置记录密钥

		return customerAccountDao.editSignPass(sId,
				sOldPassMd5,
				customerAccount.getsPaySignPass(),
				customerAccount.getiVersion(),
				customerAccount.getdModifiedDate(),
				customerAccount.getsModifieder(),
				customerAccount.getsRecordKey()
			);
	}
	
	/**
	 * 重置手势密码
	 * @param sId 被修改的用户编号
	 * @param sPass 操作人员的密码
	 * @return
	 */
	@Override
	public MsgEmity resetSignPass(String sId, String sPass) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "用户编号不能为空！", 8001);
		}
		
		sId = sId.trim();
		
		String currentCustomerId = ModuleUtil.currentLoginUserId();
		if (null == currentCustomerId || "".equals(currentCustomerId.trim())) {
			return new MsgEmity(false, "未能获取操作人员信息！", 8002);
		}

		if (!"/00000000/00000001/".contains(currentCustomerId) && !sId.equals(currentCustomerId)) {//超级管理员登录/管理员或当前用户
			return new MsgEmity(false, "仅允许管理员或当前用户重置密码", 8003);
		}
		
		MsgEmity me = customerAccountDao.findById(currentCustomerId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "当前操作用户信息错误,请联系管理员！", 8004);
		}
		
		CustomerAccount customerAccountModifieder = me.getData();

		String sModifiederPass = MD5Util.md5(sPass, appMd5Key);
		if (!(sModifiederPass.equals(customerAccountModifieder.getsPaySignPass()))) {
			return new MsgEmity(false, "提供的操作用户密码错误!", 8005);
		}
		
		me = customerAccountDao.findById(sId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到待重置密码的用户！", 8006);
		}
		
		CustomerAccount customerAccount = me.getData();

		String sOldPass = customerAccount.getsPaySignPass();
		String sNewPass = MD5Util.md5("123456", appMd5Key);
		if (sOldPass.equals(sNewPass)) {
			return MsgEmity.success(8999, "密码无需修改重置！");
		}

		customerAccount.setsPaySignPass(sNewPass);
		customerAccount.setsModifieder(ModuleUtil.currentLoginUserId());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setdModifiedDate(new Date());
		
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));//重置记录密钥

		return customerAccountDao.editSignPass(sId,
				sOldPass,
				customerAccount.getsPayPass(),
				customerAccount.getiVersion(),
				customerAccount.getdModifiedDate(),
				customerAccount.getsModifieder(),
				customerAccount.getsRecordKey()
			);
	}

	/**
	 * 验证当前账号密码
	 * @param sId 账号
	 * @param sPass 待验证密码
	 * @return
	 */
	@Override
	public MsgEmity checkPass(String sId, String sPass) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(8101, "账号参数为空");
		}

		if (null == sPass || "".equals(sPass.trim())) {
			return MsgEmity.err(8102, "待验证密码参数为空");
		}
		
		MsgEmity me = this.getValueByFieldName(sId, Arrays.asList("sPayPass", "sCreator"), true);
		if (!me.isSuccess()) {
			return me;
		}
		
		String currentCustomerId = ModuleUtil.currentLoginUserId();
		if (null == currentCustomerId || "".equals(currentCustomerId.trim())) {
			return new MsgEmity(false, "未能获取操作人员信息！", 8103);
		}
		
		CustomerAccount customerAccount = me.getData();
		
		if (!currentCustomerId.equals(customerAccount.getsCreator())) {
			return new MsgEmity(false, "账户与创建者信息不一致", 8104);
		}

		String sModifiederPass = MD5Util.md5(sPass, appMd5Key);
		if (!sModifiederPass.equals(customerAccount.getsPayPass())) {
			return new MsgEmity(false, "密码错误!", 8105);
		}
		
		return MsgEmity.success("密码正确");
	}

	/**
	 * 验证当前账号手势密码
	 * @param sId 账号
	 * @param sPass 待验证手势密码
	 * @return
	 */
	@Override
	public MsgEmity checkSignPass(String sId, String sPass) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(8101, "账号参数为空");
		}

		if (null == sPass || "".equals(sPass.trim())) {
			return MsgEmity.err(8102, "待验证手势密码参数为空");
		}
		
		MsgEmity me = this.getValueByFieldName(sId, Arrays.asList("sPaySignPass", "sCreator"), true);
		if (!me.isSuccess()) {
			return me;
		}
		
		String currentCustomerId = ModuleUtil.currentLoginUserId();
		if (null == currentCustomerId || "".equals(currentCustomerId.trim())) {
			return new MsgEmity(false, "未能获取操作人员信息！", 8103);
		}
		
		CustomerAccount customerAccount = me.getData();
		
		if (!currentCustomerId.equals(customerAccount.getsCreator())) {
			return new MsgEmity(false, "账户与创建者信息不一致", 8104);
		}

		String sModifiederPass = MD5Util.md5(sPass, appMd5Key);
		if (!sModifiederPass.equals(customerAccount.getsPaySignPass())) {
			return new MsgEmity(false, "密码错误!", 8103);
		}
		
		return MsgEmity.success("密码正确");
	}
	
	/**
	 * 减少顾客账户中的余额
	 * @param sId
	 * @param subMoney
	 * @return
	 */
	@Override
	public MsgEmity subFund(String sId, BigDecimal subMoney) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(8001, "账号参数为空");
		}
		
		if (!MoneyUtil.isPlus(subMoney)) {
			return MsgEmity.err(8002, "待减金额必须正数");
		}
		
		String sUserId = ModuleUtil.currentLoginUserId();
		if (!sId.equals(sUserId) && !"/00000000/00000001/".contains(sUserId)) {
			return MsgEmity.err(8003, "无权操作这个账户的余额");
		}
		
		MsgEmity me = customerAccountDao.findById(sId, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		CustomerAccount customerAccount = me.getData();
		
		BigDecimal mFund = MoneyUtil.sub(customerAccount.getmFund(), subMoney);
		if (MoneyUtil.isNegative(mFund)) {
			return MsgEmity.err(8004, "当前金额不足,不能减少,当前金额:", customerAccount.getmFund().toString());
		}
		
		String temp = customerAccount.createRecordKey(recordKeyJam);
		if (!temp.equals(customerAccount.getsRecordKey())) {
			return new MsgEmity(false, "账户记录验证密钥不一致！", 8005);
		}

		customerAccount.setmFund(mFund);
		customerAccount.setsModifieder(sUserId);
		customerAccount.setdModifiedDate(new Date());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));
		
		return customerAccountDao.changeFund(customerAccount);
	}
	
	/**
	 * 增加顾客账户中的余额
	 * @param sId
	 * @param incMoney
	 * @return
	 */
	@Override
	public MsgEmity incFund(String sId, BigDecimal incMoney) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(8001, "账号参数为空");
		}
		
		if (!MoneyUtil.isPlus(incMoney)) {
			return MsgEmity.err(8002, "待增金额必须正数");
		}
		
		String sUserId = ModuleUtil.currentLoginUserId();
		if (!sId.equals(sUserId) && !"/00000000/00000001/".contains(sUserId)) {
			return MsgEmity.err(8003, "无权操作这个账户的余额");
		}
		
		MsgEmity me = customerAccountDao.findById(sId, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		CustomerAccount customerAccount = me.getData();
		
		BigDecimal mFund = MoneyUtil.sum(customerAccount.getmFund(), incMoney);
		if (MoneyUtil.isNegative(mFund)) {
			return MsgEmity.err(8004, "最终金额为负数,当前金额:", customerAccount.getmFund().toString());
		}
		
		String temp = customerAccount.createRecordKey(recordKeyJam);
		if (!temp.equals(customerAccount.getsRecordKey())) {
			return new MsgEmity(false, "账户记录验证密钥不一致！", 8005);
		}

		customerAccount.setmFund(mFund);
		customerAccount.setsModifieder(sUserId);
		customerAccount.setdModifiedDate(new Date());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));
		
		return customerAccountDao.changeFund(customerAccount);
	}
	
	/**
	 * 减少顾客账户中的积分
	 * @param sId
	 * @param incTally
	 * @return
	 */
	@Override
	public MsgEmity subTally(String sId, Integer subTally) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(8001, "账号参数为空");
		}
		
		if (null == subTally || subTally.equals(0)) {
			return MsgEmity.err(8002, "无意义的操作");
		}
		
		if (subTally.intValue() < 0) {
			return MsgEmity.err(8003, "待减积分必须是正整数");
		}
		
		String sUserId = ModuleUtil.currentLoginUserId();
		if (!sId.equals(sUserId) && !"/00000000/00000001/".contains(sUserId)) {
			return MsgEmity.err(8004, "无权操作这个账户的积分");
		}
		
		MsgEmity me = customerAccountDao.findById(sId, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		CustomerAccount customerAccount = me.getData();
		
		int iTally = customerAccount.getiTally().intValue() - subTally.intValue();
		if (iTally < 0) {
			return MsgEmity.err(8005, "积分不足,不能减少,当前积分:", customerAccount.getiTally().toString());
		}
		
		String temp = customerAccount.createRecordKey(recordKeyJam);
		if (!temp.equals(customerAccount.getsRecordKey())) {
			return new MsgEmity(false, "账户记录验证密钥不一致！", 8006);
		}

		customerAccount.setiTally(iTally);
		customerAccount.setsModifieder(sUserId);
		customerAccount.setdModifiedDate(new Date());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));
		
		return customerAccountDao.changeTally(customerAccount);
	}
	
	/**
	 * 增加顾客账户中的积分
	 * @param sId
	 * @param incTally
	 * @return
	 */
	@Override
	public MsgEmity incTally(String sId, Integer incTally) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(8001, "账号参数为空");
		}
		
		if (null == incTally || incTally.equals(0)) {
			return MsgEmity.err(8002, "无意义的操作");
		}
		
		if (incTally.intValue() < 0) {
			return MsgEmity.err(8003, "待增加积分必须是正整数");
		}
		
		String sUserId = ModuleUtil.currentLoginUserId();
		if (!sId.equals(sUserId) && !"/00000000/00000001/".contains(sUserId)) {
			return MsgEmity.err(8004, "无权操作这个账户的积分");
		}
		
		MsgEmity me = customerAccountDao.findById(sId, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		CustomerAccount customerAccount = me.getData();
		
		int iTally = customerAccount.getiTally().intValue() + incTally.intValue();
		if (iTally < 0) {
			return MsgEmity.err(8005, "最终积分为负数,当前积分:", customerAccount.getiTally().toString());
		}
		
		String temp = customerAccount.createRecordKey(recordKeyJam);
		if (!temp.equals(customerAccount.getsRecordKey())) {
			return new MsgEmity(false, "账户记录验证密钥不一致！", 8006);
		}

		customerAccount.setiTally(iTally);
		customerAccount.setsModifieder(sUserId);
		customerAccount.setdModifiedDate(new Date());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));
		
		return customerAccountDao.changeTally(customerAccount);
	}
	
	/**
	 * 忘记支付密码
	 * @param sNo 识别号(电话)
	 * @param iCode 短信验证码
	 * @param sNewPass 新密码
	 * @return
	 */
	@Override
	public MsgEmity forgetPayPass(String sNo, Integer iCode, String sNewPass) {
		if (null == sNo || "".equals(sNo.trim())) {
			return MsgEmity.err(8001, "识别号(电话)参数为空");
		}

		if (null == iCode || iCode.intValue() < 1) {
			return MsgEmity.err(8002, "短信验证码参数为空");
		}
		
		if (null == sNewPass || "".equals(sNewPass.trim())) {
			return MsgEmity.err(8003, "新密码参数为空");
		}
		
		sNo = sNo.trim();
		sNewPass = sNewPass.trim();
		
		MsgEmity me = URLUtil.get(findCodeUrl.replace("电话", sNo.trim()));
		if (!me.isSuccess()) {
			return me;
		}
		
		me = JacksonShell.toJavaObject(me.getData().toString(), MsgEmity.class);
		if (!me.isSuccess()) {
			return me.appendDataCode(7300).insertMsg("验证码错误:");
		}
		
		Map<String, String> identifyingMap = me.getData();
		if (!iCode.equals(identifyingMap.get("iCode"))){
			return MsgEmity.err("验证码错误");
		}
		
		me = this.callMapping("findByNo", sNo);
		if (!me.isSuccess()) {
			return me.appendDataCode(10);
		}
		
		CustomerAccount customerAccount = me.getData();
		if (null == customerAccount) {
			return MsgEmity.err(8004, "没有找到对应的顾客数据");
		}

		String sOldPass = customerAccount.getsPayPass();
		sNewPass = MD5Util.md5(sNewPass, appMd5Key);
		if (sNewPass.equals(sOldPass)) {
			return MsgEmity.success(8999, "密码无需修改重置！");
		}

		customerAccount.setsPayPass(sNewPass);
		customerAccount.setsModifieder(ModuleUtil.currentLoginUserId());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setdModifiedDate(new Date());
		
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));//重置记录密钥

		return customerAccountDao.editPass(
				customerAccount.getsId(),
				sOldPass,
				customerAccount.getsPayPass(),
				customerAccount.getiVersion(),
				customerAccount.getdModifiedDate(),
				customerAccount.getsModifieder(),
				customerAccount.getsRecordKey()
			);
	}
	
	/**
	 * 忘记支付手势密码
	 * @param sNo 识别号(电话)
	 * @param iCode 短信验证码
	 * @param sNewPass 新密码
	 * @return
	 */
	@Override
	public MsgEmity forgetPaySignPass(String sNo, Integer iCode, String sNewPass) {
		if (null == sNo || "".equals(sNo.trim())) {
			return MsgEmity.err(8001, "识别号(电话)参数为空");
		}

		if (null == iCode || iCode.intValue() < 1) {
			return MsgEmity.err(8002, "短信验证码参数为空");
		}
		
		if (null == sNewPass || "".equals(sNewPass.trim())) {
			return MsgEmity.err(8003, "新密码参数为空");
		}
		
		sNo = sNo.trim();
		sNewPass = sNewPass.trim();
		
		MsgEmity me = URLUtil.get(findCodeUrl.replace("电话", sNo.trim()));
		if (!me.isSuccess()) {
			return me;
		}
		
		me = JacksonShell.toJavaObject(me.getData().toString(), MsgEmity.class);
		if (!me.isSuccess()) {
			return me.appendDataCode(7300).insertMsg("验证码错误:");
		}
		
		Map<String, String> identifyingMap = me.getData();
		if (!iCode.equals(identifyingMap.get("iCode"))){
			return MsgEmity.err("验证码错误");
		}
		
		me = this.callMapping("findByNo", sNo);
		if (!me.isSuccess()) {
			return me.appendDataCode(10);
		}
		
		CustomerAccount customerAccount = me.getData();
		if (null == customerAccount) {
			return MsgEmity.err(8004, "没有找到对应的顾客数据");
		}

		String sOldPass = customerAccount.getsPaySignPass();
		sNewPass = MD5Util.md5(sNewPass, appMd5Key);
		if (sNewPass.equals(sOldPass)) {
			return MsgEmity.success(8999, "密码无需修改重置！");
		}

		customerAccount.setsPaySignPass(sNewPass);
		customerAccount.setsModifieder(ModuleUtil.currentLoginUserId());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setdModifiedDate(new Date());
		
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));//重置记录密钥

		return customerAccountDao.editSignPass(
				customerAccount.getsId(),
				sOldPass,
				customerAccount.getsPaySignPass(),
				customerAccount.getiVersion(),
				customerAccount.getdModifiedDate(),
				customerAccount.getsModifieder(),
				customerAccount.getsRecordKey()
			);
	}
	
	/**
	 * 清除指定顾客账户中的积分
	 * @param sId 用户编号
	 * @return
	 */
	@Override
	public MsgEmity clearTally(String sId) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(8001, "账号参数为空");
		}
		
		MsgEmity me = customerAccountDao.findById(sId, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		CustomerAccount customerAccount = me.getData();
		
		if (0 == customerAccount.getiTally().intValue()) {
			return MsgEmity.success(8002, "当前积分为0");
		}
		
		String temp = customerAccount.createRecordKey(recordKeyJam);
		if (!temp.equals(customerAccount.getsRecordKey())) {
			return new MsgEmity(false, "账户记录验证密钥不一致！", 8006);
		}

		customerAccount.setiTally(0);
		customerAccount.setsModifieder(sId);
		customerAccount.setdModifiedDate(new Date());
		customerAccount.setiVersion(customerAccount.getiVersion() +1);
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));
		
		return customerAccountDao.changeTally(customerAccount);
	}
	
}
